package com.asia5b.a5mall.member.fragment

import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.adapter.CommentListAdapter
import com.asia5b.a5mall.member.databinding.FragmentCommentlistBinding
import com.asia5b.a5mall.middleware.base.SimpleFragment
import com.asia5b.a5mall.middleware.event.PostCommentEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.member.CommentListFragmentVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.member.CommentsModel
import com.asia5b.a5mall.middleware.model.bean.CommentMultiItemEntity
import com.asia5b.a5mall.middleware.utils.*
import kotlinx.android.synthetic.main.fragment_commentlist.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CommentListFragment : SimpleFragment<CommentListFragmentVM, FragmentCommentlistBinding>() {
    override val viewModelClazz: Class<CommentListFragmentVM>
        get() = CommentListFragmentVM::class.java

    override fun getDataBinding(container: ViewGroup?): FragmentCommentlistBinding {
        return FragmentCommentlistBinding.inflate(layoutInflater, container, false)
    }

    val auto by lazy { auto_commented }
    val mRecyclerView by lazy { rv_commented }
    val mDatas = arrayListOf<CommentMultiItemEntity>()
    val mAdapter by lazy { CommentListAdapter(mDatas, state) }
    val emptyView by lazy { newEmptyView(mActivity) }

    //state 状态 1：待评价订单,2:待追评,3:已评价
    var state = 1


    override fun initView() {
        if(arguments!=null) {
            state = requireArguments().getInt("type", 1)
        }
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
            if (state != 1) {
                addItemDecoration(newDivider(mActivity))
            }
        }
    }

    override fun initEvent() {
        auto.setOnLoadMoreListener {
            getDatas(2)
        }

        auto.setOnRefreshListener {
            getDatas(1)
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = mAdapter.getItem(position) as CommentMultiItemEntity
            if (item is CommentsModel) {
                if (state == 2 && view.id == R.id.tv_commentlist_watch) {
                    push(ActCons.CommentAgainActivity, {
                        it.withSerializable("datas", arrayListOf(item))
                    })
                } else if (state == 3 && view.id == R.id.tv_commentlist_watch) {
                    push(ActCons.CommentDetailActivity, {
                        it.withSerializable("id", item.comment_id)
                    })
                }
            }
        }
    }

    override fun startObserver() {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun lazyFetchData() {
        getDatas(0)
    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh, state)

    }


    fun getDatasResult(resultModel: ResultModel<ArrayList<CommentMultiItemEntity>>) {
        if (emptyView.parent == null) {
            mAdapter.setEmptyView(emptyView)
        }
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (resultModel.data != null) {
            mAdapter.addData(resultModel.data!!)
        }

        auto.setEnableLoadMore(resultModel.page < resultModel.sumPage)
    }

    override val isRegisterEventBus: Boolean
        get() = true

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPostCommentEvent(event: PostCommentEvent) {
        getDatas(1)
    }
}